package com.xiang.plane.model;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;

import com.xiang.plane.util.ImageUtil;
import com.xiang.plane.util.PVector;
import com.xiang.plane.util.Timer;
import com.xiang.plane.util.TypeConst;
import com.xiang.plane.view.GameView;

/**
 * Created by XY on 2017/8/9.
 * 游戏玩家
 */

public class Player {
    private static final String TAG = "Player";
    //血条
    private int left = 10;
    //飞机坐标
    private int playerX, playerY;
    //飞机的图片
    private Bitmap planeBit;
    //飞机的宽高
    private int planeWidthHalf, planeHeight;
    //发射子弹的CD
    private Timer fireTime = new Timer(5);
    //发射子弹的类型
    private int bulletLevel = TypeConst.Player_Bullet_LeveOne;
    //是否在爆炸
    private boolean isExplode = false;
    //加载的资源的Resources
    private Resources res;

    public Player(Resources res) {
        this.res = res;
        this.planeBit = ImageUtil.ImageCacahe.get(ImageUtil.HERO_1_KEY);

        planeWidthHalf = planeBit.getWidth() / 2;
        planeHeight = planeBit.getHeight();

        playerX = GameView.screenW / 2 - planeWidthHalf;
        playerY = GameView.screenH - planeHeight - 50;
    }

    public void setPlayerX(int playerX) {
        this.playerX = playerX;
    }

    public void setPlayerY(int playerY) {
        this.playerY = playerY;
    }

    public float getPlayerX() {
        return playerX;
    }

    public float getPlayerY() {
        return playerY;
    }

    public float getPlaneWidthHalf() {
        return planeWidthHalf;
    }

    public float getPlaneHeight() {
        return planeHeight;
    }

    /**
     * 绘制飞机
     * @param canvas
     * @param paint
     */
    public void draw(Canvas canvas, Paint paint) {
        if (isExplode) {//绘制爆炸动画

        } else {
            canvas.drawBitmap(planeBit, playerX, playerY, paint);
        }
    }

    /**
     * 飞机逻辑
     *  1.发射子弹
     *  2.检查生命
     */
    public void logic() {
        //子弹cd到，就发射子弹
        if (fireTime.act()) {
            fire();
        }
        switch (GameView.Score) {
            case 1:
                bulletLevel = TypeConst.Player_Bullet_LeveTwo;
                break;
            case 2:
                bulletLevel = TypeConst.Player_Bullet_LeveThree;
                break;
            case 3:
                bulletLevel = TypeConst.Player_Bullet_LeveFour;
                break;
            case 4:
                bulletLevel = TypeConst.Player_Bullet_LeveFive;
                break;
            case 5:
                bulletLevel = TypeConst.Player_Bullet_LeveSix;
                break;
        }
    }

    /**
     * 发射子弹
     */
    private void fire() {
        switch (bulletLevel) {
            case TypeConst.Player_Bullet_LeveOne:
                new Bullet(res,playerX + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                break;
            case TypeConst.Player_Bullet_LeveTwo:
                new Bullet(res,playerX + planeWidthHalf - 20, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                new Bullet(res,playerX + planeWidthHalf + 20, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                break;

            case TypeConst.Player_Bullet_LeveThree:
                new Bullet(res,playerX + planeWidthHalf - 40, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                new Bullet(res,playerX + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                new Bullet(res,playerX + planeWidthHalf + 40, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                break;

            case TypeConst.Player_Bullet_LeveFour:
                new Bullet(res,playerX + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
                break;

            case TypeConst.Player_Bullet_LeveFive:
                new Bullet(res,playerX + planeWidthHalf - 70, playerY, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
                new Bullet(res,playerX + planeWidthHalf + 70, playerY, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
                break;

            case TypeConst.Player_Bullet_LeveSix:
                new Bullet(res,playerX + planeWidthHalf - 70, playerY, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
                new Bullet(res,playerX + planeWidthHalf - 30, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                new Bullet(res,playerX + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                new Bullet(res,playerX + planeWidthHalf + 30, playerY - 35, false, TypeConst.MY_BULLET_NORMAL_1);
                new Bullet(res,playerX + planeWidthHalf + 70, playerY, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
                break;
        }


//        switch (this.bulletType) {
//            //普通子弹
//            case TypeConst.MY_SHOT_SINGLE_BULLET_1:
//                //发射我军子弹
//                new Bullet(res,playerX + planeWidthHalf, playerY - 35, false, bulletType);
//                break;
//            //散弹
//            case TypeConst.MY_SHOT_MULITI_BULLET_1:
//                makeMulitiBullet(5, 5);
//                break;
//            //跟踪弹1号
//            case TypeConst.MY_SHOT_VECTOR_TRACK_1:
//                for (int i = 1; i <= 5; i++) {
//                    new Bullet(res,playerX - i * 10 + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
//                }
//                new Bullet(res,playerX + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
//                for (int i = 1; i <= 5; i++) {
//                    new Bullet(res,playerX + i * 10 + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
//                }
//                break;
//            //跟踪弹2号
//            case TypeConst.MY_SHOT_VECTOR_TRACK_2:
//                //new Bullet(res,playerX - 10 + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(-5, 1));
//                new Bullet(res,playerX + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(0, -1));
//                //new Bullet(res,playerX + 10 + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_VECTOR_TRACK).setInitV(new PVector(5, 1));
//                break;
//            //贝塞尔子弹
//            case TypeConst.MY_SHOT_BZ_BULLET_1:
//                new Bullet(res,playerX - 20 + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_BZ_TRACK_1).setInitV(new PVector(0, 0));
//                new Bullet(res,playerX + 20 + planeWidthHalf, playerY - 35, false, TypeConst.MY_BULLET_BZ_TRACK_2).setInitV(new PVector(0, 0));
//                break;
//        }
    }

    /**
     * 产生多颗子弹
     *
     * @param bulletNum  子弹数量
     * @param amongAngle 子弹角度的间隔
     */
    private void makeMulitiBullet(int bulletNum, int amongAngle) {
        //先建一颗垂直向上的子弹
        new Bullet(res,this.playerX + planeWidthHalf, this.playerY - 10, false, TypeConst.MY_BULLET_LINE).setInitV(new PVector(0, -1));
        //判断左右偏转
        boolean flag = false;
        //用来计算偏移角度
        int num = 1;
        while (bulletNum-- > 0) {
            int rotateNum = num / 2;
            //设置初始向量
            PVector initV = new PVector(0, -1);
            //如果flag是true就将向量向左边偏转，否则向右偏转
            if (flag)
                initV.rotateAngle(amongAngle * rotateNum);
            else
                initV.rotateAngle(-amongAngle * rotateNum);
            //新建一个旋转的直线向量子弹
            new Bullet(res,this.playerX + planeWidthHalf, this.playerY - 10, false, TypeConst.MY_BULLET_LINE).setInitV(initV);
            //转换标记
            flag = !flag;
            num++;
        }
    }
}
